package com.binvshe.binvshe.message;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.message.dao.MessageHelper;
import com.pachong.android.baseuicomponent.activity.BaseActivity;

import butterknife.Bind;
import butterknife.OnClick;

public class MessageIndexActivity extends BaseActivity {


    @Bind(R.id.friendRedPoint)
    View friendRedPoint;

    @Bind(R.id.commontRedPoint)
    View commontRedPoint;

    @Bind(R.id.dianzanRedPoint)
    View dianzanRedPoint;

    @Bind(R.id.systemRedPoint)
    View systemRedPoint;


    /**
     * 好友消息被点击了
     */
    @OnClick(R.id.lytFriends)
    public void onFriendMsgClicked() {
        Intent Intent = new Intent(this, FriendMessageActivity.class);
        startActivity(Intent);
    }

    /**
     * 点赞消息被点击了
     */
    @OnClick(R.id.lytDianzan)
    public void onDianzanMsgClicked() {
        LikeMessageActivity.start(this);
    }

    /**
     * 评论消息被点击了
     */
    @OnClick(R.id.lytComment)
    public void onCommentMsgClicked() {
        ReplyMessageActivity.start(this);
    }

    /**
     * 系统消息被点击了
     */
    @OnClick(R.id.lytSystem)
    public void onSystemMsgClicked() {
        SystemMessageActivity.start(this);
    }

    public static void start(Context context) {
        Intent starter = new Intent(context, MessageIndexActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolBar();
    }

    private void initToolBar() {
        setTitle("消息");
        enableBackButton();

    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_message_index, null);
    }


    @Override
    protected void onResume() {
        super.onResume();
        updateUnreadState();
    }


    /**
     * todo
     */
    private void updateUnreadState() {
        // 获取“好友”消息的未读数
        int friendUnreadNum1 = MessageHelper.getInstance().loadUnreadNumType(MessageType.MSG_FRIEND_REQUEST);
        int friendUnreadNum2 = 0;//MessageHelper.getInstance().loadUnreadNumType(MessageType.MSG_FRIEND_REQUEST_REJECTED);
        int friendUnreadNum3 = 0;//MessageHelper.getInstance().loadUnreadNumType(MessageType.MSG_FRIEND_REQUEST_ACCEPTED);

        if (friendUnreadNum1 > 0 || friendUnreadNum2 > 0 || friendUnreadNum3 > 0) {
            friendRedPoint.setVisibility(View.VISIBLE);
        } else {
            friendRedPoint.setVisibility(View.INVISIBLE);
        }

        // 获取“评论”消息的未读数
        int unreadNumOfComment = MessageHelper.getInstance().loadUnreadNumType(MessageType.MSG_TYPE_COMMENT);
        if (unreadNumOfComment > 0) {
            commontRedPoint.setVisibility(View.VISIBLE);
        } else {
            commontRedPoint.setVisibility(View.INVISIBLE);
        }


        // 获取“点赞”消息的未读数
        int unreadNumOfLike = MessageHelper.getInstance().loadUnreadNumType(MessageType.MSG_TYPE_LIKE_ARTICAL);
        int unreadNumOfLikeComment = MessageHelper.getInstance().loadUnreadNumType(MessageType.MSG_TYPE_LIKE_COMMENT);
        if (unreadNumOfLike > 0 || unreadNumOfLikeComment > 0) {
            dianzanRedPoint.setVisibility(View.VISIBLE);
        } else {
            dianzanRedPoint.setVisibility(View.INVISIBLE);
        }

        // 获取“系统/其他”消息的未读数
        int unreadNum1 = MessageHelper.getInstance().loadUnreadNumType(MessageType.MSG_TYPE_INVITE);
        int unreadNum2 = MessageHelper.getInstance().loadUnreadNumType(MessageType.MSG_TYPE_REQUEST);
        int unreadNum3 = MessageHelper.getInstance().loadUnreadNumType(MessageType.MSG_TYPE_OTHERS);
        int unreadNum4 = MessageHelper.getInstance().loadUnreadNumType(MessageType.MSG_TYPE_CLUB);

        if (unreadNum1 > 0 || unreadNum2 > 0 || unreadNum3 > 0 || unreadNum4 > 0) {
            systemRedPoint.setVisibility(View.VISIBLE);
        } else {
            systemRedPoint.setVisibility(View.INVISIBLE);
        }

    }


}
